/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReservaNueva.java
 *
 * Created on 21/10/2010, 01:15:42 AM
 */

package pe.edu.upla.lamerced.desk.view.hospedaje;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.Utiles;
import pe.edu.upla.lamerced.bean.Habitacion;
import pe.edu.upla.lamerced.bean.Hospedaje;
import pe.edu.upla.lamerced.bean.Tipohabitacion;
import pe.edu.upla.lamerced.bean.Users;
import pe.edu.upla.lamerced.desk.view.Login;
import pe.edu.upla.lamerced.service.HabitacionService;
import pe.edu.upla.lamerced.service.HospedajeService;
import pe.edu.upla.lamerced.service.UsuarioService;

/**
 *
 * @author elvis
 */
public class ReservaNueva extends javax.swing.JInternalFrame {

   private JDesktopPane escritorio;
   private HospedajeService hospedajeService;
   private HabitacionService habitacionService;
   private UsuarioService usuarioService;
   private List<Habitacion> listaHabitaciones;
   private List<Users> listaClientes;

   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }
    /** Creates new form ReservaNueva */
    public ReservaNueva() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cbtipohabitacion = new javax.swing.JComboBox();
        cbHabitacion = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtDias = new javax.swing.JTextField();
        txtFecha = new javax.swing.JTextField();
        btnNuevaReserva = new javax.swing.JButton();
        btnCancelaReserva = new javax.swing.JButton();
        cbClientes = new javax.swing.JComboBox();

        setClosable(true);
        setTitle("Registrar Reserva");

        jLabel1.setText("Cliente");

        jLabel2.setText("Tipo de habitacion");

        jLabel3.setText("Habitacion");

        cbtipohabitacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbtipohabitacion.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbtipohabitacionItemStateChanged(evt);
            }
        });

        cbHabitacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Dias");

        jLabel5.setText("Fecha");

        btnNuevaReserva.setText("Grabar");
        btnNuevaReserva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaReservaActionPerformed(evt);
            }
        });

        btnCancelaReserva.setText("Cancelar");
        btnCancelaReserva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelaReservaActionPerformed(evt);
            }
        });

        cbClientes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel3))
                                    .addGap(13, 13, 13))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(btnNuevaReserva)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                            .addComponent(jLabel1))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbClientes, 0, 166, Short.MAX_VALUE)
                            .addComponent(cbHabitacion, 0, 166, Short.MAX_VALUE)
                            .addComponent(cbtipohabitacion, 0, 166, Short.MAX_VALUE)
                            .addComponent(txtFecha, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(txtDias, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(btnCancelaReserva, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(cbClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(cbtipohabitacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cbHabitacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtDias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelaReserva)
                    .addComponent(btnNuevaReserva))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelaReservaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelaReservaActionPerformed
       // TODO add your handling code here:
       ReservaLista form = new ReservaLista();
       form.setEscritorio(escritorio);
       escritorio.add(form);
       form.cargaInicial();
       form.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_btnCancelaReservaActionPerformed

    private void btnNuevaReservaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaReservaActionPerformed
       // TODO add your handling code here:
       try {
			Hospedaje hos = new Hospedaje();
			hos.setFecReserva(Utiles.stringToDate(txtFecha.getText(), Utiles.FORMATO_FECHA_CORTA));
			hos.setDiasreserva(Integer.parseInt(txtDias.getText()));
			Habitacion hab = listaHabitaciones.get(cbHabitacion.getSelectedIndex());
			hos.setHabitacion(hab);
			hos.setEstado(1);
			Hospedaje rsertmp = hospedajeService.obtenerReservaExacta(txtFecha.getText(),hab.getIdhabitacion().toString());
            Users ulogin = Login.getUserLogin();
            if( rsertmp != null){
               //TODO validar si es que el cliente de la reserva  es el cliente del hospedaje
               if(!rsertmp.getUsersByIdUsuarioCli().getUsername().equals(ulogin.getUsername())){
                  JOptionPane.showMessageDialog(this, "ya esta reservada por otro cliente en esa fecha");
                  return;
               }
            }
            //if(ulogin.getTipo()==1){
               hos.setUsersByIdUsuarioCli(listaClientes.get(cbClientes.getSelectedIndex()));
               hos.setUsersByIdUsuarioEmp(ulogin);
			//}
			hospedajeService.registrarReserva(hos);
			hos = hospedajeService.obtenerReserva(hos.getIdhospedaje());

                         JOptionPane.showMessageDialog(this, "informacion grabada", "Confirmacion", JOptionPane.INFORMATION_MESSAGE);
               ReservaLista form = new ReservaLista();
               form.setEscritorio(escritorio);
               escritorio.add(form);
               form.cargaInicial();
               form.setVisible(true);
               this.dispose();
        } catch (Exception e) {
             JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
             e.printStackTrace();
        }
      
    }//GEN-LAST:event_btnNuevaReservaActionPerformed

    private void cbtipohabitacionItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbtipohabitacionItemStateChanged
       // TODO add your handling code here:
       if(cbtipohabitacion.getSelectedIndex() != -1){
          int tip = cbtipohabitacion.getSelectedIndex();
          Habitacion hab = new Habitacion();
          hab.setEstado(0);
          hab.setTipohabitacion(habitacionService.listaTipoHabitacion().get(tip));
          cbHabitacion.removeAllItems();
          listaHabitaciones = habitacionService.listaHabitacionessinEstado(hab);
          System.out.println("ELEMENTOS = "+listaHabitaciones.size());
          for(Habitacion habitacion : listaHabitaciones){
             cbHabitacion.addItem(habitacion.getNumero());
          }
       }
    }//GEN-LAST:event_cbtipohabitacionItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelaReserva;
    private javax.swing.JButton btnNuevaReserva;
    private javax.swing.JComboBox cbClientes;
    private javax.swing.JComboBox cbHabitacion;
    private javax.swing.JComboBox cbtipohabitacion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField txtDias;
    private javax.swing.JTextField txtFecha;
    // End of variables declaration//GEN-END:variables

   public void cargaInicial(){
      if(hospedajeService==null){
        hospedajeService = (HospedajeService)Inicial.getFactory().getBean("hospedajeService");
      }
      if(habitacionService==null){
         habitacionService = (HabitacionService)Inicial.getFactory().getBean("habitacionService");
      }
      if(usuarioService==null){
         usuarioService = (UsuarioService)Inicial.getFactory().getBean("usuarioService");
      }
      cbClientes.removeAllItems();
      listaClientes = usuarioService.listaClientes();
      for(Users usuario : listaClientes){
         cbClientes.addItem(usuario.getNombrecompleto());
      }
      cbtipohabitacion.removeAllItems();
      for(Tipohabitacion th : habitacionService.listaTipoHabitacion()){
         cbtipohabitacion.addItem(th.getNombre());
      }
   }
}
